package com.thy.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.common.base.PageResult;
import com.hospital.common.dto.audit.module.SaveLogDto;
import com.hospital.common.pojo.TAuitLog;
import com.hospital.common.vo.audit.module.AdminVo;
import com.hospital.common.vo.audit.module.AuitLogVo;
import com.thy.aop.LogAction;
import com.thy.aop.LogTargetType;
import com.thy.mapper.TAuitLogMapper;
import com.thy.service.TAuitLogService;
import com.thy.utils.AuitLogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 审核记录表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-09-28
 */
@Service
public class TAuitLogServiceImpl extends ServiceImpl<TAuitLogMapper, TAuitLog> implements TAuitLogService {
    @Autowired
    private TAuitLogMapper tAuitLogMapper;
    @Override
    public PageResult<AuitLogVo> getAllLog(Integer pageNum, Integer pageSize,Long adminId) {
        Page<AuitLogVo> page = new Page<>(pageNum,pageSize);
        IPage<AuitLogVo> auitLogVos = tAuitLogMapper.getAllLog(page, adminId);
        if (!auitLogVos.getRecords().isEmpty()){
            for (AuitLogVo record : auitLogVos.getRecords()) {
                LogAction logAction = LogAction.getById(record.getActionId());
                if (logAction != null){
                    record.setAction(logAction.getAction());
                }else {
                    record.setAction("非法行为");
                }
                LogTargetType logTargetType = LogTargetType.getById(record.getTargetTypeId());
                if (logTargetType != null){
                    record.setTargetTypeName(logTargetType.getType());
                }else {
                    record.setTargetTypeName("非法类型");
                }
                if (record.getTargetTypeId() == 4L){
                    //目标类型为板块时根据目标id获取板块名称
                    record.setTargetName(tAuitLogMapper.getSectionName(record.getTargetId()));
                }else {
                    //目标类型为其他类型时根据目标id获取目标名称
                    record.setTargetName(tAuitLogMapper.getTargetName(record.getTargetId()));
                }
            }
        }
            return new PageResult<>(auitLogVos.getTotal(), auitLogVos.getRecords());
    }

    @Override
    public List<AdminVo> getAllAdmin() {
        return tAuitLogMapper.getAllAdmin();
    }

    @Override
    public void saveSectionLog(SaveLogDto saveLogDto) {
        AuitLogUtil.logSection(saveLogDto.getAdminId(),saveLogDto.getSectionId(),saveLogDto.getUrl());
    }
}
